import { _decorator, Component, Node, Vec3, Animation, tween, Sprite } from 'cc';
import { EventName, prop_config } from '../manager/AppControll';
import { SoundManager } from '../manager/SoundManger';
import EvtManger from '../tools/EvtManger';
import { Loader } from '../tools/LoadManger';
const { ccclass, property } = _decorator;

@ccclass('DropMaterial')
export class DropMaterial extends Component {
    private mEndPoint: Vec3 = null;
    private mNSpeed: number = 1000;
    private mSprite: Sprite = null;
    private mConfig: prop_config = null;

    protected onLoad(): void {
        this.mSprite = this.node.getChildByName("Material").getChildByName("GunMaterial").getComponent(Sprite);
        this.InitView();
    }

    InitView() {
        Loader.GetTurnTableRewardImage("rewardIcon/" + this.mConfig.propIcon, (spf) => {
            this.mSprite.spriteFrame = spf;
            this.node.getComponent(Animation).play();
        })
    }

    InitDropMaterial(pos: Vec3, config: prop_config) {
        this.mEndPoint = pos;
        this.mConfig = config;
        this.node.getComponent(Animation).on(Animation.EventType.FINISHED, () => {
            this.DoMove();
        })
    }

    DoMove() {
        SoundManager.Instance<SoundManager>().PlayEffect("ui/debrisFly");
        let time = this.mEndPoint.clone().subtract(this.node.position).length() / this.mNSpeed;
        const dropAnimation = tween()
            .to(time, { position: this.mEndPoint })
            .call(() => {
                //this.AddSkinfragments();
            });
        tween(this.node)
            .then(dropAnimation.clone(this.node))
            .start();
        this.scheduleOnce(() => {
            this.node.destroy();
        }, time + 0.01);
    }
}


